package HLLib.game;

import HLCode.HLClass;
import HLCode.HLClassManager;
import HLCode.HLClassManager_H;
import HLCode.HLObject;
import HLLib.base.HLDictionary;
import HLLib.base.HLImageManager;
import HLLib.base.HLInt;
import HLLib.base.HLIntList;
import HLLib.base.HLList;
import HLLib.base.HLLoading;
import HLLib.base.HLString;
import HLLib.io.HLFile;

public class HLSpriteSeedManager extends HLList {
    public HLClass GetClass(HLClassManager classManager) {
        return classManager.GetLibClass(HLClassManager_H.CLASS_GROUP_TYPE_LIB_GAME, HLClassManager_H.LIB_CLASS_GAME_SpriteSeedManager);
    }

    public int GetInt(int index) {
        return super.GetInt(index);
    }

    public void SetInt(int index, int value) {
        super.SetInt(index, value);
    }

    public boolean GetBoolean(int index) {
        return super.GetBoolean(index);
    }

    public void SetBoolean(int index, boolean value) {
        super.SetBoolean(index, value);
    }

    public HLObject GetObject(int index) {
        switch (index) {
        case 0: {
            return imageManager;
        }
        }
        return super.GetObject(index);
    }

    public void SetObject(int index, HLObject value) {
        switch (index) {
        case 0: {
            imageManager = (HLImageManager) value;
            return;
        }
        }
        super.SetObject(index, value);
    }

    public HLImageManager imageManager;

    static HLSpriteLoadInfo spriteLoadInfoStatic;

    HLSpriteLoadInfo spriteLoadInfo;

    static HLDictionary spriteNamesStatic;

    HLDictionary spriteNames;

    public HLSpriteSeedManager() {
        if (spriteLoadInfoStatic == null) {
            spriteLoadInfoStatic = new HLSpriteLoadInfo();
            spriteLoadInfoStatic.Load("spriteload.cfg");
        }
        spriteLoadInfo = spriteLoadInfoStatic;

        if (spriteNamesStatic == null) {
            spriteNamesStatic = new HLDictionary();
            {
                HLFile file = null;
                //HLFile file = new HLFile("spritenames.txt");
//                if (file.data != null) {
//                    HLList spriteNameList = HLString.ValueOfBytes(file.data).Split(',');
//                    for (int i = 0; i < spriteNameList.Count(); i++) {
//                        HLInt tempInt = new HLInt();
//                        tempInt.theValue = i;
//                        spriteNamesStatic.Put((HLString) spriteNameList.GetItem(i), tempInt);
//                    }
//                }
            }
        }
        spriteNames = spriteNamesStatic;

        items = new HLSpriteSeed[spriteLoadInfo.SpriteTypeCount];
    }

    public HLSpriteSeedManager(HLSpriteLoadInfo spriteLoadInfo) {
        this.spriteLoadInfo = spriteLoadInfo;

        items = new HLSpriteSeed[spriteLoadInfo.SpriteTypeCount];
    }

    public int GetTypeByName(HLString name) {
        return ((HLInt) spriteNames.GetItem(name)).theValue;
    }

    public void Load(int type) {
        if (items[type] == null) {
            HLSpriteSeed spriteSeed = new HLSpriteSeed(this);
            spriteSeed.Load(type, true, true);
            items[type] = spriteSeed;
        }
    }

    public void LoadEX(int type, boolean needOpenImagePack, boolean needCloseImagePack) {
        if (items[type] == null) {
            HLSpriteSeed spriteSeed = new HLSpriteSeed(this);
            spriteSeed.Load(type, needOpenImagePack, needCloseImagePack);
            items[type] = spriteSeed;
        }
    }

    public void Loads(int startIndex, int endIndex, int endProgress) {
        int startProgress = HLLoading.GetProgress();
        int dProgress = endProgress - startProgress;
        for (int i = startIndex; i <= endIndex; i++) {
            HLLoading.SetProgress(startProgress + dProgress * (i - startIndex) / (endIndex - startIndex + 1));
            LoadEX(i, true, false);
        }
        HLLoading.SetProgress(endProgress);
        HLImageManager.CloseImagePackage();
    }

    public void LoadList(HLIntList list, int endProgress) {
        int startProgress = HLLoading.GetProgress();
        int dProgress = endProgress - startProgress;
        for (int i = 0; i < list.items.length; i++) {
            HLLoading.SetProgress(startProgress + dProgress * i / list.items.length);
            LoadEX(list.items[i], true, false);
        }
        HLLoading.SetProgress(endProgress);
        HLImageManager.CloseImagePackage();
    }

    public void LoadAll(int endProgress) {
        int startProgress = HLLoading.GetProgress();
        int dProgress = endProgress - startProgress;
        for (int i = 0; i < spriteLoadInfo.SpriteTypeCount; i++) {
            HLLoading.SetProgress(startProgress + dProgress * i / spriteLoadInfo.SpriteTypeCount);
            LoadEX(i, true, false);
        }
        HLLoading.SetProgress(endProgress);
        HLImageManager.CloseImagePackage();
    }

    public void UnLoad(int type) {
        if (items[type] != null) {
            ((HLSpriteSeed) items[type]).UnLoadImages();
            items[type] = null;
        }
    }

    public void Clear() {
        for (int i = 0; i < items.length; i++) {
            UnLoad(i);
        }
    }

    public HLSprite CreateSprite(int type) {
        return HLSprite.Create((HLSpriteSeed) items[type]);
    }

    public HLSpriteSeed GetSeed(int index) {
        return (HLSpriteSeed) items[index];
    }

    public void SetSeed(int index, HLSpriteSeed item) {
        items[index] = item;
    }
}
